<template>
  <div class="permission-container">
    <div class="app-container">
      <!-- 表格 -->
      <el-card>
        <div style="text-align: right; margin-bottom: 20px">
          <el-button type="primary" size="small" @click="clickAdd(1, '0')">
            添加权限
          </el-button>
        </div>
        <!-- 这里的row-key=id不是固定的，id应该是数据里面的唯一值而且不能为空和不能为0，否则会报错或者展开后不能折叠 -->
        <el-table border :data="list" row-key="id">
          <el-table-column label="名称" prop="name" />
          <el-table-column label="标识" prop="code" />
          <el-table-column label="描述" prop="description" />
          <el-table-column label="操作">
            <template #default="{ row }">
              <!-- {{ row }} -->
              <el-button
                v-if="row.type === 1"
                size="small"
                type="text"
                @click="clickAdd(2, row.id)"
              >
                添加权限点
              </el-button>
              <el-button
                size="small"
                type="text"
                @click="clickShowEdit(row.id)"
              >
                查看
              </el-button>
              <el-button size="small" type="text" @click="clickDel(row.id)">
                删除
              </el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-card>
    </div>
    <el-dialog :title="title" :visible.sync="showDialog" @close="closeDialog">
      <el-form :model="form" label-width="100px">
        <el-form-item label="权限名称">
          <el-input v-model="form.name" />
        </el-form-item>
        <el-form-item label="权限标识">
          <el-input v-model="form.code" />
        </el-form-item>
        <el-form-item label="权限描述">
          <el-input v-model="form.description" />
        </el-form-item>
        <el-form-item label="权限启用">
          <el-switch
            v-model="form.enVisible"
            active-text="启用"
            active-value="1"
            inactive-text="不启用"
            inactive-value="0"
          />
        </el-form-item>
      </el-form>
      <template #footer>
        <div style="text-align: right;">
          <el-button @click="closeDialog">
            取 消
          </el-button>
          <el-button type="primary" @click="clickSubmit">
            确 定
          </el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import {
  addPermissionApi,
  delPermissionApi,
  getPermissionDetailApi,
  getPermissionListApi,
  updatePermissionApi
} from '@/api/permisson'
import { tranListToTreeData } from '@/utils'
export default {
  name: 'Permissions',
  data() {
    return {
      list: [],
      showDialog: false,
      form: {
        enVisible: '0', // 开启
        name: '', // 名称
        code: '', // 权限标识
        description: '', // 描述
        type: '', // 类型
        pid: '' // 添加到哪个节点下
      },
      title: ''
    }
  },
  created() {
    // 获取树状数据方便渲染
    this.getPermissionList()
  },
  methods: {
    // 添加权限按钮点击
    clickAdd(type, pid) {
      this.showDialog = true
      this.form.type = type
      this.form.pid = pid
      this.title = '添加权限'
    },
    // 确认按钮
    async clickSubmit() {
      // 这一步要初始化
      if (this.form.name === '' || this.form.code === '') {
        // console.log(1)
        this.$message.error('权限名称和权限标识不能为空')
        return
      }
      // 判断查看更新和添加权限
      if (this.form.id) {
        const {
          data: { message }
        } = await updatePermissionApi(this.form)
        this.$message.success(message)
      } else {
        const {
          data: { message }
        } = await addPermissionApi(this.form)
        this.$message.success(message)
      }

      this.getPermissionList()
      this.closeDialog()
    },
    // 删除
    clickDel(id) {
      this.$confirm('此操作将永久删除该权限, 是否继续?', '提示', {
        type: 'warning'
      })
        .then(async () => {
          await delPermissionApi(id)
          // console.log(res)
          this.getPermissionList()
          this.$message({
            type: 'success',
            message: '删除权限点成功'
          })
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },
    // 点击查看
    async clickShowEdit(id) {
      const {
        data: { data }
      } = await getPermissionDetailApi(id)
      // console.log(data)
      this.showDialog = true
      this.form = data
      this.title = '查看编辑'
    },
    // 关闭弹出
    closeDialog() {
      this.showDialog = false
      this.form = {
        enVisible: '0', // 开启
        name: '', // 名称
        code: '', // 权限标识
        description: '', // 描述
        type: '', // 类型
        pid: '' // 添加到哪个节点下
      }
    },
    // 获取树状数据
    async getPermissionList() {
      const {
        data: { data }
      } = await getPermissionListApi()
      // console.log(data)
      this.list = tranListToTreeData(data, '0')
    }
  }
}
</script>
